package cn.thr.pethome.menu.controller;

import cn.thr.pethome.annotation.PreAuthorize;
import cn.thr.pethome.menu.domain.Menu;
import cn.thr.pethome.menu.query.MenuQuery;
import cn.thr.pethome.menu.service.IMenuService;
import cn.thr.pethome.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;

    @GetMapping("/tree")
    public List<Menu> findAllByparentId(){
       return menuService.findAllByparentId();
    }


    @GetMapping("/role")
    public List<Menu> tree(){
        System.out.println(menuService.tree());
        return menuService.tree();
    }

    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "菜单新增修改",value = "menu:save")
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null){
                menuService.update(menu);
            }
            else{
                menuService.add(menu);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "菜单删除",value = "menu:delent")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            menuService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "菜单查询一个",value = "menu:findOne")
    public Menu get(@PathVariable("id")Long id)
    {
        return menuService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "菜单的查全部",value = "menu:findAll")
    public List<Menu> list(){

        return menuService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "菜单的分页",value = "menu:list")
    public AjaxResult json(@RequestBody MenuQuery query) {

        try {
            //获取到当前页
            List<Menu> byQuery = menuService.findByQuery(query);
            //这是获取到总条数
            Long count = menuService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
            //setData 用来返回数据的   setTotal 用来返回总条数
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PatchMapping("/del")
    @PreAuthorize(name = "菜单的批量删除",value = "menu:batchDel")
    //@RequestBody 把后端都数据返回给前端
    public  AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            menuService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }
}
